package com.juncheng.controller;

import com.juncheng.controller.utils.Result;
import com.juncheng.domain.Book;
import com.juncheng.service.IBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private IBookService bookService;
    @GetMapping
    public Result getAll(){
        log.info("查询所有图书");
        return new Result(true, bookService.list());
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
       log.info("查询id为{}的图书");
       return new Result(true, bookService.getById(id));
    }
    @PostMapping
    public Result save(@RequestBody Book book){
        log.info("保存图书{}", book);
        return new Result(bookService.save(book));
    }
    @PutMapping
    public Result update(@RequestBody Book book){
        log.info("修改图书{}", book);
        return new Result(bookService.updateById(book));
    }
    /**
     * 删除图书
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除id为{}的图书", id);
        return new Result(bookService.removeById(id));
    }
    @GetMapping("/{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize){
        log.info("分页查询：{}，{}" , currentPage + " " , pageSize);
        return new Result(true, bookService.getPage(currentPage, pageSize));
    }
}
